#include "../include/Server.h"
#include "../include/HttpuUp.h"
#include "../include/GetRtspstream.h"
#include "../include/RegistGetusername.h"
#include "../include/RegisterGetcrypt.h"
#include "../include/LoginGetusername.h"
#include "../include/LoginGetcrypt.h"
#include <iostream>

enum TaskType{
    TASK_LOGIN_SECTION1 = 1, 
    TASK_LOGIN_SECTION1_RESP_OK,
    TASK_LOGIN_SECTION1_RESP_ERROR,
    TASK_LOGIN_SECTION2,
    TASK_LOGIN_SECTION2_RESP_OK,
    TASK_LOGIN_SECTION2_RESP_ERROR,
    TASK_TYPE_REGISTER1,
    TASK_TYPE_REGISTER1_RESP_OK,
    TASK_TYPE_REGISTER1_RESP_ERROR,
    TASK_TYPE_GEGISTER2,
    TASK_TYPE_REGISTER2_RESP_OK,
    TASK_TYPE_HTTP_UP
    TASK_TYPE_GET_RTSP_STREAM,
};   

using std::endl;
using std::cout;

Server::Server(int threadNums, int threadTaskQueueSize, 
               string ip, unsigned short port)
    :_threadpool(threadNums, threadTaskQueueSize)
     ,_tcpServer(ip, port)
{

    using namespace std::placeholders;
    _tcpServer.setAllCallbacks(
                               std::bind(&Server::_onClose, this, _1)
                               ,std::bind(&Server::_onMessage,this,_1)
                               ,std::bind(&Server::_onConnection,this,_1)
                              );
}
void Server::start()
{
    _threadpool.start();
    _tcpServer.start();
}
void Server::stop()
{
    _tcpServer.stop();
    _threadpool.stop();
}

void Server::_onConnection(TcpConnectionPtr conn)
{
    cout << conn->toString() << " has connected " << endl;
}
void Server::_onMessage(TcpConnectionPtr conn)
{
    int taskType;
    string msg;
    //接收任务
    conn->recv(&taskType, msg);
    cout << "Server.cpp _onmessage()53, tasktype = " << taskType << ", msg = " << msg << endl;
    switch(taskType)
    {
    case TASK_TYPE_REGISTER1:
        {
            //封装任务
            Task * registerGetusername = new RegisterGetusername(msg,conn);
            //交给线程池处理
            _threadpool.addTask(registerGetusername);
            break;
        }
    case TASK_TYPE_GEGISTER2:
        {
            //封装任务
            Task * registerGetcrypt= new RegisterGetcrypt(msg,conn);
            //交给线程池处理
            _threadpool.addTask(registerGetcrypt);
            break;
        }
    case TASK_LOGIN_SECTION1:
        {
            Task * logingetusername = new LoginGetusername(msg, conn);
            _threadpool.addTask(logingetusername);
            break;
        }
    case TASK_LOGIN_SECTION2:
        {
            Task * logingetcrypt= new LoginGetcrypt(msg, conn);
            _threadpool.addTask(logingetcrypt);
            break;
        }
    case TASK_TYPE_HTTP_UP:
        {
            Task  *httpup = new HttpUp(msg, conn);
            _threadpool.addTask(httpup);
            break;
        }
    case TASK_TYPE_GET_RTSP_STREAM:
        {
            Task  *getrtspstream = new GetRtspstream(msg, conn);
            _threadpool.addTask(getrtspstream);
            break;
        }
    default:
        cout << "no tasktype" << endl;
    }
}
void Server::_onClose(TcpConnectionPtr conn)
{
    cout << conn->toString() << " has disconnected " << endl;
}
